package com.cly.config;

import com.cly.constant.QueueConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class OrderMQConfig {

    //---------商品修改库存的队列---------
    @Bean
    public Queue prodChangeQueue(){
        return new Queue(QueueConstant.PROD_CHANGE_QUEUE);
    }

    @Bean
    public DirectExchange prodChangeEx(){
        return new DirectExchange(QueueConstant.PROD_CHANGE_EX);
    }

    @Bean
    public Binding prodChangeBind(){
        return BindingBuilder.bind(prodChangeQueue()).to(prodChangeEx()).with(QueueConstant.PROD_CHANGE_KEY);
    }


    //------延迟队列----------

    /**
     * 延迟队列
     * 消息就往这个队列里面放
     * @return
     */
    @Bean
    public Queue prodMsQueue(){
        //配置消息死亡后走哪里
        Map<String, Object> map = new HashMap<>();
        //死亡后走哪个交换机
        map.put("x-dead-letter-exchange", QueueConstant.PROD_DEAD_EX);
        //死亡后走哪个路由key
        map.put("x-dead-letter-routing-key", QueueConstant.PROD_DEAD_KEY);
        //死亡时间
        map.put("x-message-ttl", 120 * 1000);
        return new Queue(QueueConstant.PROD_MS_QUEUE, true, false, false, map);
    }

    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue prodDeadQueue(){
        return new Queue(QueueConstant.PROD_DEAD_QUEUE);
    }

    /**
     * 商品的死信交换机
     * @return
     */
    @Bean
    public DirectExchange prodDeadEx(){
        return new DirectExchange(QueueConstant.PROD_DEAD_EX);
    }

    @Bean
    public Binding prodDeadBind(){
        return BindingBuilder.bind(prodDeadQueue()).to(prodDeadEx()).with(QueueConstant.PROD_DEAD_KEY);
    }

}
